package com.example.androidlearning2200;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

public class DialogActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog);
        findViewById(R.id.btnAlertDialog).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popAlertDialog();
            }
        });
        findViewById(R.id.btnProgressDialog).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popProgressDialog();
            }
        });
        findViewById(R.id.btnCustomDialog).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popCustomDialog();
            }
        });
    }

    /**
     * 弹出自定义对话框
     */
    private void popCustomDialog() {
        Dialog dialog = new Dialog(this);
        dialog.setContentView(R.layout.layout_login);
        // 设置dialog宽高
        Window window = dialog.getWindow();
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.width = (int) (getResources().getDisplayMetrics().widthPixels * 0.8);
        window.setAttributes(layoutParams);
        // 设置白色圆角背景
        GradientDrawable drawable = new GradientDrawable();
        drawable.setColor(Color.WHITE);
        drawable.setCornerRadius(32);
        window.setBackgroundDrawable(drawable);
        dialog.show();
    }

    /**
     * 弹出进度对话框
     */
    private void popProgressDialog() {
        ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setTitle("下载");
        progressDialog.setMessage("正在下载，请稍后...");
        //progressDialog.setCancelable(false);
        progressDialog.show();
        //progressDialog.cancel();
    }

    /**
     * 弹出警示框
     */
    private void popAlertDialog() {
        // Builder 建造者模式，链式调用
        new AlertDialog.Builder(this).setTitle("温馨提示").setMessage("上课不刻苦，别人吃饭你跳舞！").setPositiveButton("好的", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Toast.makeText(DialogActivity.this, "我要刻苦学习", Toast.LENGTH_LONG).show();
            }
        }).setNegativeButton("别管我", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Toast.makeText(DialogActivity.this, "我喜欢跳舞", Toast.LENGTH_LONG).show();
            }
        }).create().show();
    }

}